---
title: "Kerr Final"
author: "Tom Kerr"
date: "November 3, 2018"
output: pdf_document
---

```{r  setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, cache = T)
library(lme4)
library(merTools)
library(arm)
library(dplyr)
library(ggplot2)
library(knitr)
library(texreg)
library(caTools)
library(plyr)
library(foreign)
library(readxl)
library(sp)
library(rgdal)
library(nlme)
library(knitLatex)
library(latexpdf)
library(xtable)
library(stargazer)
library(pander)
library(pscl)
library(plotly)
library(stringr)
library(lubridate)
library(ltm)
library(MCMCpack)
library(ggmcmc)
library(stm)
library(wordcloud)
library(tm)
library(quanteda)
library(rvest)
library(poLCA)
library(cluster)
library(psych)
library(readr)
library(dplyr)
library(rgr)
library(tidyverse)  
library(factoextra) 
library(dendextend)
library(fpc)
library(XML)
library(RCurl)
library(SnowballC)
library(randomForest)
library(tree)
library(igraph)
library(amen)
library(mlbench)
library(haven)
library(discretization)
library(glmnet)
library(pbmcapply)
library(tidyverse)
library(tidygraph)
library(ggraph)
library(igraph)
library(pdp)
library(gbm)
library(reshape2)
library(reshape)
library(network)
library(MASS)

options( warn = -1 )

setwd("C:/Users/thoma/Desktop/UNC/Thesis_Research/Data")


```

Data Manipulation

```{r, eval=F, echo=T, message = FALSE, warning = FALSE, error = FALSE}
#RAND's Big Armed and Dangerous Dataset

#Remove Unrelated Variables
BAAD <- BAAD[,-c(11:18)]
BAAD <- BAAD[,-c(1,3,4)]
BAAD <- BAAD[,-7]
BAAD <- BAAD[,-7]

#Revalue Factor Variables to Numeric 
BAAD$ordsize <- revalue(BAAD$ordsize, c("0 - 100 & low confidence"="1"))
BAAD$ordsize <- revalue(BAAD$ordsize, c("100 - 1,000"="2"))
BAAD$ordsize <- revalue(BAAD$ordsize, c("1,000 - 10,000"="3"))
BAAD$ordsize <- revalue(BAAD$ordsize, c("10,000 or more"="4"))
BAAD$ordsize <- as.factor(BAAD$ordsize)

BAAD$terrStrong <- revalue(BAAD$terrStrong, c("No"="0"))
BAAD$terrStrong <- revalue(BAAD$terrStrong, c("Yes"="1"))
BAAD$terrStrong <- as.factor(BAAD$terrStrong)

#Limit observations to only groups who have inflicted 100 or more fatalities
BAAD <- BAAD[ which(BAAD$fatalities19982005>='100'), ]

save(BAAD, file = "BAAD_mod.RData")

#Global Terrorism Dataset

#Remove Unrelated Variables
gtd <- gtd[,-c(1:8)]
gtd <- gtd[,-c(4:11)]
gtd <- gtd[,-c(10:12)]
gtd <- gtd[,-c(12:15)]
gtd <- gtd[,-c(76:98)]
gtd <- gtd[,-c(77:84)]
gtd <- gtd[,-c(20:35)]
gtd <- gtd[,-c(21:25)]
gtd <- gtd[,-c(22:27)]
gtd <- gtd[,-c(23:31)]
gtd <- gtd[,-c(25:40)]
gtd <- gtd[,-c(14:17)]
gtd <- gtd[,-c(23:24)]
gtd <- gtd[,-c(2:7)]
gtd <- gtd[,-c(3:5)]
gtd <- gtd[,-c(8:9)]
gtd <- gtd[,-12]

#Filter to include only insurgencies 
gtd <- gtd[ which(gtd$alternative=='1'), ]

#Filter to remove unknown weapon type
gtd <- gtd[!(gtd$weaptype1 == "13"),]
gtd <- gtd[!(gtd$weaptype1 == "12"),]
gtd <- gtd[!(gtd$weaptype1 == "9"),]

#Filter to include only observations of domestic groups without international members
gtd <- gtd[(gtd$INT_LOG == "0"),]

#Filter to include only observations of domestic groups attacking central government
gtd <- gtd[(gtd$INT_IDEO == "0"),]

save(gtd, file = "gtd_mod.RData")

#Non-State Actors in Civil War Dataset

nsa <- nsa_v3.4_21November2013

#Remove Unrelated Variables
nsa <- nsa[, -c(1:3)]
nsa <- nsa[, -c(4:8)]
nsa <- nsa[, -17]
nsa <- nsa[, -c(5:6)]
nsa <- nsa[, -10]
nsa <- nsa[, -15]
nsa <- nsa[, -c(14:25)]
nsa <- nsa[, -c(17:18)]

#Set dummy variable for territory control
nsa$terrcont <- revalue(nsa$terrcont, c("no"="0"))
nsa$terrcont <- revalue(nsa$terrcont, c("yes"="1"))

#Revalue and Recode Termination Variables 
nsa$type.of.termination <- as.factor(nsa$type.of.termination)
nsa$victory.side <- as.factor(nsa$victory.side)

nsa <- nsa[ which(!nsa$type.of.termination == '7'),]
nsa <- nsa[ which(!nsa$type.of.termination == '6'),]
nsa <- nsa[ which(!nsa$type.of.termination == '6.1'),]


nsa$type.of.termination <- revalue(nsa$type.of.termination, c("2"="1"))
nsa$type.of.termination <- revalue(nsa$type.of.termination, c("3"="1"))
nsa$type.of.termination <- revalue(nsa$type.of.termination, c("5"="1"))

nsa <- within(nsa, type.of.termination[type.of.termination == "4" & victory.side == "2"] <- "1")

nsa$type.of.termination <- revalue(nsa$type.of.termination, c("4"="0"))

nsa <- nsa[, -15]

#Remove incomplete cases
nsa <- na.omit(nsa)


save(nsa, file = "nsa.RData")


```


```{r,eval=F, echo=T, message = FALSE, warning = FALSE, error = FALSE}
load("BAAD_mod.RData")

load("gtd_mod.RData")

load("nsa.RData")

#Primary Model

mod <- glmer(type.of.termination ~  armsproc + terrcont + fightcap + rebestimate + rebstrength + prevactive  + (1 | side_b), data = nsa, family = binomial(link = 'logit'))

stargazer(mod)

stargazer(mod, 
          title            = "Binomial Results for Termination Outcome",
          dep.var.labels   = "Probability of Termination Favoring ASG")


#Supplemental Graphs

ggplot(data=BAAD, aes(x=terrStrong, y=fatalities19982005, fill=OrgAge)) + 
    geom_bar(colour="black", stat="identity",
             position=position_dodge(),
             size=.3) +                        
    scale_fill_hue(name="Age of Organization") +      
    xlab("Control of Territory") + ylab("Fatalities Inflicted by Group") + 
    ggtitle("Influence of Controlled Terrirory and Organizational Age on Lethality") +     
    theme_bw()

#Remove Groups Associated with the Invasion of Afghistan and Iraq
aq <- aq[-11,]

aq2 <- aq[-11,]

aq3 <- aq2[-14,]

aq4 <- aq3[-19,]

aq5 <- aq4[-18,]

aq6 <- aq5[-7,]



p <- ggplot(data=aq6, aes(x=terrStrong, y=fatalities19982005, fill=OrgAge)) + 
    geom_bar(colour="black", stat="identity",
             position=position_dodge(),
             size=.3) +                        
    scale_fill_hue(name="Age of Organization") +      
    xlab("Control of Territory") + ylab("Fatalities Inflicted by Group") + 
    ggtitle("Groups Formed After OEF OIF Excluded") +     
    theme_bw()

p + theme(legend.position="bottom")

```

